package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class BibliotecaApp {

    public static void main(String[] args) {
        Login login = initializeLogin();
        User currentUser = checkLogin(login);

        menu(currentUser);
    }

    private static User checkLogin(Login login) {
        boolean rightCredentials = false;
        while(!rightCredentials) {
            String userLogin = "";
            String userPassword = "";
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("If you want to leave type '0'");
            System.out.print("Type your library number: ");

            try {
                userLogin = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (userLogin.equals("0"))
                System.exit(0);

            System.out.print("Type your password: ");

            try {
                userPassword = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (userPassword.equals("0"))
                System.exit(0);

            if(login.checkCredentials(userLogin, userPassword))
                rightCredentials = true;

            printBreakLines(1);
        }

        return login.getCurrentUser();

    }

    private static Login initializeLogin() {
        User firstUser = new User("login", "senha", "maria", "maria@maria.com", "2222-3333");
        User secondUser = new User("another_login", "another_pass", "Joao", "Joao@maria.com", "2233-4433");
        ArrayList<User> users = new ArrayList<User>();
        users.add(firstUser);
        users.add(secondUser);
        return new Login(users);
    }

    private static void menu(User currentUser) {
        printStartMessage();

        Menu menu = new Menu(currentUser);

        print(menu.showMainMenu());

        String menuOption = "";
        while(!menuOption.equals("0")) {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Digite a opcao:");

            try {
                menuOption = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }

            if(!menuOption.equals("0")) {
                MessageMenu messageMenu = menu.chooseMenu(menuOption);
                print(messageMenu);
            }

            printBreakLines(1);
        }
    }

    public static String showStartMessage() {
        return "Hello, welcome to Biblioteca!";
    }

    public static void printStartMessage() {
        System.out.println("*************************************************************");
        System.out.println(showStartMessage());
        System.out.println("*************************************************************");
        printBreakLines(2);
    }

    public static void print(MessageMenu menu) {
        if (!menu.getMessage().equals("")){
            System.out.println(menu.getMessage());
            printBreakLines(3);
        }

        if (!menu.getUserInformation().equals("")){
            System.out.println(menu.getUserInformation());
            printBreakLines(2);
        }

        int position = 0;
        int itensListSize = menu.getItens().size();
        if(itensListSize > 0) {
            System.out.println("-----------------------------------------------------------");
            System.out.println("LIST OF AVAILABLE ITENS");
            while (position < itensListSize) {
                System.out.println(menu.getItens().get(position).toString());

                ++position;
            }
            System.out.println("-----------------------------------------------------------");
            printBreakLines(1);
        }

        position = 0;
        while (position < menu.getMenu().size()) {
            System.out.println(menu.getMenu().get(position).toString());

            ++position;
        }
    }

    public static void printBreakLines(int times){
        for(int i = 0; i < times; ++i){
            System.out.println("");
        }
    }
}
